package com.cl.common.api;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description 通用返回对象
 * @Author cr7
 * @Date 2022/2/21
 * @Version V1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult {

    @ApiModelProperty(value = "响应码")
    private Integer code;

    @ApiModelProperty(value = "响应信息")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private Object data;

    /**
     * 成功返回结果
     *
     * @return
     */
    public static CommonResult success() {
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }

    /**
     * 成功返回结果(带数据)
     *
     * @param data
     * @return
     */
    public static CommonResult success(String message, Object data) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @return
     */
    public static CommonResult fail() {
        return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
    }

    /**
     * 失败返回结果(带消息)
     *
     * @param message
     * @return
     */
    public static CommonResult fail(String message) {
        return new CommonResult(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     *
     * @return
     */
    public static CommonResult unauthorized() {
        return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), null);
    }

    /**
     * 未授权返回结果
     *
     * @return
     */
    public static CommonResult forbidden() {
        return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), null);
    }

}
